package demo.letcode.top100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _两数之和 {
    /**
     *
     */
    public static void main(String[] args) {
        int[] arr = {-2, 1, 4, 3, 2, -7, 5};
        Arrays.sort(arr);
        List<List<Integer>> list = new ArrayList<>();
        int tar = 3;
        int k = arr.length - 1;
        for (int i = 0; i < arr.length; i++) {
            int l = i + 1;
            while (l < k) {
                int sum = arr[i] + arr[l] + arr[k];
                if (sum == tar) {
                    List<Integer> ll = new ArrayList<>();
                    ll.add(arr[i]);
                    ll.add(arr[l]);
                    ll.add(arr[k]);
                    list.add(ll);
                    while (l < k && arr[l] == arr[l + 1]) {
                        l++;
                    }
                    while (l < k && arr[k] == arr[k - 1]) {
                        k--;
                    }
                    l++;
                    k--;
                } else if (sum > tar) {
                    k--;
                } else {
                    l++;
                }
            }
        }
        System.out.println(list);
    }
}
